package com.kele.controller.AliPayController;

import com.kele.dao.UserDao;
import com.kele.domain.Order;
import com.kele.domain.User;
import com.kele.service.OrderService;
import com.kele.service.UserService;
import com.kele.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * @author 赵奥林
 * @date 2021年06月12日 16:55
 * @Description 支付记录管理控制器
 */
@RequestMapping("/payLog")
@Controller
@Api(value = "支付宝支付模块", tags = {"支付宝支付相关接口"})
public class PayLogController {

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    /**
     * 查看所有支付记录
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        List<Order> allOrders = orderService.findAllOrders();
        model.addAttribute("orders", allOrders);
        return "/static_resources/jsp/allOrders";
    }

    /**
     * 支付表单提交
     * @param order
     * @return 生成订单记录
     */
    @RequestMapping(value = "/addPayLog",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建新的订单",notes = "新增订单到数据库记录")
    public ResponseData addPayLog(Order order, @ApiIgnore HttpSession session) {
        if (order == null) {
            return ResponseData.fail("订单已存在");
        }
        //生成订单号（uuid）
        order.setOrder_id(PayLogController.generateSn("KELE",""));
        orderService.insertOrder(order);
        session.setAttribute("order",order);
        return ResponseData.success("添加订单成功",order);
    }

    /**
     * 生成可带前后锥的唯一字符串，作为订单编号
     * @param prefix
     * @param suffix
     * @return
     */
    public static String generateSn(String prefix, String suffix) {
        return prefix + UUID.randomUUID().toString().replace("-", "").toUpperCase() + suffix;
    }

    /**
     * 完成支付，重定向到开通vip方法
     * @return
     */
    @RequestMapping(value = "/endPay",method = RequestMethod.GET)
    public String endPay(){
        return "redirect:/payLog/endOrder";
    }

    /**
     * 返回订单,完成充值
     * @return
     */
    @RequestMapping(value = "/endOrder", method = RequestMethod.GET)
    public String endOrder(HttpServletRequest request) {
        Integer id= ((User)request.getSession().getAttribute("User")).getId();
        BigDecimal money=((Order) request.getSession().getAttribute("order")).getOrder_amount();
        //充值完成，设置用户vip到期时间
        DateTime dateTime = new DateTime();
        switch (money.intValue()) {
            case 10:
                DateTime dateTime1 = dateTime.plusMonths(1);
                userService.setVip(id, dateTime1.toString("yyyy-MM-dd hh:mm:ss"), 1);
                break;
            case 30:
                DateTime dateTime2 = dateTime.plusMonths(4);
                userService.setVip(id, dateTime2.toString("yyyy-MM-dd hh:mm:ss"), 1);
            default:
                DateTime dateTime3=dateTime.plusDays(7);
                userService.setVip(id,dateTime3.toString("yyyy-MM-dd hh:mm:ss"),1);
        }
        return "redirect:../to/toVipCenter";
    }

}
